<template>
  <ContentWrap>
    <div :class="searchVisible ? 'flex flex-row flex-wrap mb-2' : 'flex flex-row flex-wrap'">
      <el-button v-if="showBack" @click="router.back()">
        <Icon icon="ep:arrow-left" class="mr-5px" />返回
      </el-button>
      <el-button plain type="success" @click="searchVisible = !searchVisible">
        <Icon class="mr-5px" icon="ep:operation" />
        {{ searchVisible ? '关闭筛选' : '打开筛选' }}
      </el-button>
      <el-text type="info">
        <span class="ml-5px">{{ searchVisible ? '关闭以下筛选条件' : '点击打开筛选条件' }}</span>
      </el-text>
      <div class="flex-1"></div>
      <el-button @click="resetQuery">
        <Icon class="mr-5px" icon="ep:refresh" />
        刷新列表
      </el-button>
    </div>
    <el-form v-if="searchVisible" ref="searchFormRef" :model="tableData.searchParam" inline>
      <el-form-item label="状态">
        <el-select
          class="w-[280px]"
          v-model="tableData.searchParam.status"
          clearable
          placeholder="请选择状态"
        >
          <el-option label="全部" value="" />
          <el-option label="待结算" value="0" />
          <el-option label="已结算" value="1" />
          <el-option label="已失效" value="2" />
          <el-option label="已取消" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="tableData.searchParam.createTime"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <div class="mt-[5px]">
      <el-table :data="tableData.list" size="large" v-loading="tableData.loading">
        <template #empty>
          <span>{{ !tableData.loading ? '没有相关数据' : '' }}</span>
        </template>
        <el-table-column label="交易单号" min-width="120">
          <template #default="{ row }">
            <div>{{ row.tradeNo || '无' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="订单金额" min-width="90">
          <template #default="{ row }">
            <div class="text-orange-500">¥{{ fenToYuan(row.orderPrice) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="实付金额" min-width="90">
          <template #default="{ row }">
            <div class="text-orange-500">¥{{ fenToYuan(row.payPrice) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="结算金额" min-width="90">
          <template #default="{ row }">
            <div class="text-orange-500">¥{{ fenToYuan(row.settlementMoney) }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          min-width="170"
        />
        <el-table-column label="备注" min-width="180">
          <template #default="{ row }">
            <div>{{ row.note }}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="100" align="center" fixed="right">
          <template #default="{ row }">
            <el-tooltip
              v-if="row.status == 1"
              :content="'已于' + formatTime(row.unfreezeTime) + '结算'"
            >
              <el-tag type="success">已结算</el-tag>
            </el-tooltip>
            <el-tooltip
              v-else-if="row.status == 2"
              :content="'已于' + formatTime(row.cancelTime) + '失效'"
            >
              <el-tag type="warning">已失效</el-tag>
            </el-tooltip>
            <el-tooltip
              v-else-if="row.status == 3"
              :content="'已于' + formatTime(row.cancelTime) + '取消'"
            >
              <el-tag type="warning">已取消</el-tag>
            </el-tooltip>
            <el-tooltip v-else :content="'将于' + formatTime(row.unfreezeTime) + '结算'">
              <el-tag type="primary">待结算</el-tag>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        v-model:limit="tableData.searchParam.pageSize"
        v-model:page="tableData.searchParam.pageNo"
        :total="tableData.total"
        @pagination="loadTableData"
      />
    </div>
  </ContentWrap>
</template>

<script lang="ts" setup name="RevenueList">
import * as RevenueApi from '@/api/shop/revenue/RevenueApi'
import { FormInstance } from 'element-plus'
import { fenToYuan, formatTime } from '@/utils'
import { dateFormatter } from '@/utils/formatTime'

const router = useRouter()

const searchVisible = ref(false)

const tableData = reactive({
  total: 0,
  loading: true,
  list: [],
  searchParam: {
    pageNo: 1,
    pageSize: 10,
    status: undefined,
    createTime: []
  }
})

const loadTableData = () => {
  tableData.loading = true
  RevenueApi.getPage({
    ...tableData.searchParam
  })
    .then((res: any) => {
      tableData.loading = false
      tableData.list = res.list
      tableData.total = res.total
    })
    .catch(() => {
      tableData.loading = false
    })
}

const searchFormRef = ref<FormInstance>()

const handleQuery = () => {
  tableData.searchParam.pageNo = 1
  loadTableData()
}

const resetQuery = () => {
  searchFormRef.value?.resetFields()
  handleQuery()
}

const showBack = ref(false)
onMounted(() => {
  handleQuery()
})
</script>
